﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.BIZ;
using KBManagement.Library;

namespace KBManagement
{
    public partial class FindCustomer : Form
    {
        KBSQLClassDataContext context;
        CustomerFacade customerFacade;
        int dgvCustomerIndex = 0;
        public FindCustomer()
        {
            context = Utils.CreateInstance().GetDBContext();
            customerFacade = new CustomerFacade();
            InitializeComponent();
            SelectCustomer("all");
            Customer customer = new Customer();
            customer = customerFacade.FindByPhone("0");
            if (customer != null)
            {
                Utils.CreateInstance().NotificationCustomerObserver(customer);
            }
        }

        private void SelectCustomer(String mode)
        {
            
            if (mode.Equals("name"))
            {
                var customer = from fc in context.Customers where fc.FullName.Contains(textBox1.Text)
                               select new
                               {
                                   fc.ID,
                                   fc.FullName,
                                   fc.Email,
                                   fc.Adddress,
                                   fc.Phone,
                               };
                dataGridView1.DataSource = customer;
            }
            else if (mode.Equals("phone"))
            {
                var customer = from fc in context.Customers where fc.Phone.Contains(textBox1.Text)
                               select new
                               {
                                   fc.ID,
                                   fc.FullName,
                                   fc.Email,
                                   fc.Adddress,
                                   fc.Phone,
                               };
                dataGridView1.DataSource = customer;
            }
            else
            {
                var customer = from fc in context.Customers
                               select new
                               {
                                   fc.ID,
                                   fc.FullName,
                                   fc.Email,
                                   fc.Adddress,
                                   fc.Phone,
                               };
                dataGridView1.DataSource = customer;
            }
            dataGridView1.Columns[0].HeaderText = "Mã số";
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].HeaderText = "Họ tên";
            dataGridView1.Columns[2].HeaderText = "Thư điện tử";
            dataGridView1.Columns[3].HeaderText = "Địa chỉ";
            dataGridView1.Columns[4].HeaderText = "Điện thoại";
            try
            {
                dataGridView1.CurrentCell = dataGridView1[0, dgvCustomerIndex];
            }
            catch (Exception)
            {

            }
            //employeeQuery = from fc in context.Employees select fc;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
         
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Customer customer = new Customer();
                customerFacade = new CustomerFacade();
                customer = customerFacade.FindByPhone(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                Utils.CreateInstance().NotificationCustomerObserver(customer);
                Close();
            }
            catch (Exception)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!radioButton1.Checked && !radioButton2.Checked)
            {
                MessageBox.Show("Lựa chọn loại tìm kiếm");
                return;
            }

            if (radioButton1.Checked)
            {
                SelectCustomer("name");
            }
            else if(radioButton2.Checked)
            {
                SelectCustomer("phone");  
            }
        }
    }
}
